//
//  PPCacheManager.m
//  PPDataBase_Example
//
//  Created by ppss on 2021/6/17.
//  Copyright © 2021 ppss. All rights reserved.
//

#import "PPCacheManager.h"
#import <YYKit/YYKit.h>


static NSString *const PPDataBaseCacheKey = @"PPDataBaseCache";

@interface PPCacheManager()

@property (nonatomic, strong) YYCache *cacheManager;

@end

@implementation PPCacheManager


-(YYCache *)cacheManager{
    if (!_cacheManager) {
        NSString *libDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
        NSString *cacheDir = [libDir stringByAppendingFormat:@"/%@/%@", [[NSBundle mainBundle] bundleIdentifier], PPDataBaseCacheKey];
        _cacheManager = [YYCache cacheWithPath:cacheDir];
    }
    return _cacheManager;
}


//数据是否存在cache中
- (BOOL)containsObjectWithParams:(NSDictionary *)params{
    
    NSString *keyStr =  params[@"key"];
    
    return [self.cacheManager containsObjectForKey:keyStr];
}
//存储数据
- (void)setCacheWithParams:(NSDictionary *)params{
    
    id data =  params[@"data"];

    NSString *keyStr =  params[@"key"];

    [self.cacheManager setObject:data forKey:keyStr];

}

//获取数据
- (id)getCacheWithParams:(NSDictionary *)params{
    NSString *keyStr =  params[@"key"];

    return [self.cacheManager objectForKey:keyStr];
}

//移除数据
- (void)removeCacheWithParams:(NSDictionary *)params{
    NSString *keyStr =  params[@"key"];

    [self.cacheManager removeObjectForKey:keyStr];

}

- (void)removeAllCache{
    [self.cacheManager.diskCache removeAllObjects];
}

- (NSInteger)getAllCacheSize{
    return [self.cacheManager.diskCache totalCost];
    
}

#pragma mark - 内存持久化
//存储数据
- (void)setMemoryCacheWithParams:(NSDictionary *)params{
 
    id data =  params[@"data"];

    NSString *keyStr =  params[@"key"];
    
    [self.cacheManager.memoryCache setObject:data forKey:keyStr];
}

//获取数据
- (id)getMemoryCacheWithParams:(NSDictionary *)params{

    NSString *keyStr =  params[@"key"];

    return [self.cacheManager.memoryCache objectForKey:keyStr];

}
@end
